package com.java.basis.thread.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangning
 * @create 2021-04-25 22:05
 */
public class TestCAS01 {
	public static void main(String[] args) {
		//无锁，自旋锁
		AtomicInteger atomicInteger = new AtomicInteger();
//		int i = atomicInteger.intValue();
//		System.out.println("i = " + i);

		for (int j = 0; j < 10; j++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					System.out.println("i = " + atomicInteger.getAndAdd(2));
				}
			}).start();
		}
		for (int j = 0; j < 10; j++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					System.out.println("i = " + atomicInteger.getAndIncrement());
				}
			}).start();
		}
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("i = " + atomicInteger.getAndIncrement());
			}
		});
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("i = " + atomicInteger.getAndIncrement());
			}
		});
		t1.start();
		t2.start();

	}
}
